﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class BiaoDuanGuanLi : BaseForm
    {
        public BiaoDuanGuanLi()
        {
            InitializeComponent();
        }

        private void BiaoDuanGuanLi_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView3.AutoGenerateColumns = false;
            date_kaishishijian.Value = DateTime.Today;
            date_jieshushijian.Value = date_kaishishijian.Value.AddHours(23).AddMinutes(59).AddSeconds(59);
            BindInfo();
        }

        private void menu_1_xinzeng_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("标段管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            BiaoDuanBianJi biaoDuanBianJi = new BiaoDuanBianJi(tabControl1.SelectedIndex);
            if (biaoDuanBianJi.ShowDialog() == DialogResult.OK)
            {
                BindInfo();
            }
        }

        private void menu_1_xiugai_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("标段管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }

            if (tabControl1.SelectedTab.Text == "路基")
            {
                if (dataGridView1.SelectedRows.Count == 0)
                {
                    return;
                }
                int selectIndex = dataGridView1.SelectedRows[0].Index;
                BiaoDuanBianJi biaoDuanBianJi = new BiaoDuanBianJi(dataGridView1.SelectedRows[0],tabControl1.SelectedIndex);
                if (biaoDuanBianJi.ShowDialog() == DialogResult.OK)
                {
                    BindInfo();
                }
            }
            if (tabControl1.SelectedTab.Text =="其他")
            {
                if (dataGridView3.SelectedRows.Count == 0)
                {
                    return;
                }
                int selectIndex = dataGridView3.SelectedRows[0].Index;
                BiaoDuanBianJi biaoDuanBianJi = new BiaoDuanBianJi(dataGridView3.SelectedRows[0], tabControl1.SelectedIndex);
                if (biaoDuanBianJi.ShowDialog() == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }

        private void menu_1_shanchu_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("标段管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView1.SelectedRows .Count==0)
            {
                return;
            }
            if (KeaoUtils.ShowQuestionMessage("是否要删除该标段信息？") == DialogResult.Yes)
            {
                if (KeaoUtils._DB.DeleteRowByID(new TableRow() { TableName = "tbl_biaoduan", ID = string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value) }) > 0)
                {
                    KeaoUtils.ShowInfoMessage("删除标段信息完成。");
                    BindInfo();
                }
            }
        }

        private void BindInfo()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("biaoduanleixing", DataValueCommonType.字符串, "路基", "", SearchConditionType.等于);
            createCondition += KeaoUtils._DB.CreateAndCondition("riqi", DataValueCommonType.日期时间, date_kaishishijian.Value.ToString("yyyy-MM-dd HH:mm:ss"), date_jieshushijian.Value.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
            DataTable Dt1 = KeaoUtils._DB.GetDataTableByCondition("tbl_biaoduan",createCondition);
            dataGridView1.DataSource = Dt1;
            createCondition = KeaoUtils._DB.CreateAndCondition("biaoduanleixing", DataValueCommonType.字符串, "其他", "", SearchConditionType.等于);
            DataTable Dt2 = KeaoUtils._DB.GetDataTableByCondition("tbl_biaoduan", createCondition);
            dataGridView3.DataSource = Dt2;
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value =string.Format("{0}" ,e.Row.Index + 1);
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
    }
}
